package com.example.lyl.yuejian.view;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.RegisterResultBean;
import com.example.lyl.yuejian.net.NetTool;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class RegisterActivity extends AppCompatActivity {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.regist_user_et)
    EditText registUserEt;
    @BindView(R.id.regist_psw_et)
    EditText registPswEt;
    @BindView(R.id.regist_psw_agin_et)
    EditText registPswAginEt;
    @BindView(R.id.register_btn)
    Button registerBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        toolbar.setTitle("注册");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            //是左上角按键出现
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    @OnClick(R.id.register_btn)
    public void onViewClicked() {
        NetTool.getInstance().getApi().
                registerRequest(registUserEt.getText().toString(),registPswEt.getText().toString(),registPswAginEt.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<RegisterResultBean>() {
                    @Override
                    public void accept(RegisterResultBean registerResultBean) throws Exception {
                        if (registerResultBean.getCode().equals("1")){
                            Toast.makeText(RegisterActivity.this,registerResultBean.getMessage(), Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(RegisterActivity.this, registerResultBean.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Toast.makeText(RegisterActivity.this, "检查网络连接", Toast.LENGTH_SHORT).show();
                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return  true;
    }

}
